package com.qch.edu.Map;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author qch
 * @Date 2021/4/13
 *
 * 并发
 */
public class BinfaHashMapTest {
    public static void main(String[] args) {
        HashMap<Integer,Integer> map=new HashMap<Integer, Integer>();
        ConcurrentHashMap<Integer,Integer> concurrentHashMap=new ConcurrentHashMap<Integer, Integer>();
        for (int i = 0; i < 1000; i++) {
            map.put(i,i);
        }
        new Thread(new AddThread(map)).start();
        new Thread(new RemoveThread(map)).start();
        new Thread(new InteratorThread(map)).start();

    }

}
class AddThread implements Runnable{
   Map<Integer,Integer> hashmap;

    public AddThread(Map<Integer, Integer> hashmap) {
        this.hashmap = hashmap;
    }

    public void run() {
        while (true){
            int random=new Random().nextInt();
            hashmap.put(random,random);
        }
    }
}
class RemoveThread implements Runnable{
    Map<Integer,Integer> hashmap;

    public RemoveThread(Map<Integer, Integer> hashmap) {
        this.hashmap = hashmap;
    }

    public void run() {
        while (true){
            int random=new Random().nextInt();
            hashmap.remove(random);
        }
    }
}
class InteratorThread implements Runnable{
    Map<Integer,Integer> hashmap;

    public InteratorThread(Map<Integer, Integer> hashmap) {
        this.hashmap = hashmap;
    }

    public void run() {
        System.out.println("-------------------------"+hashmap.size());
        for (Integer integer : hashmap.values()) {
            System.out.println("value"+integer);
        }
        System.out.println("-------------------------"+hashmap.size());
    }
}